package com.service;

import java.text.DecimalFormat;
import java.util.Iterator;

import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.entity.CheckerDdata;
import com.entity.CheckerMdata;
import com.entity.PowerRateRecovery;

/**
 *  @Description: 大屏系统调用营销业务系统接口实现类
 *  @author  SLH
 *  @date  201710206
 *
 */
@Service("WsService")
@Transactional
public class WsServiceImpl extends CommonServiceImpl implements IWsService {
	public static final String ORG_NO = "311001";

	@Override
	public void saveTeamPowerRateRecovery(String content) {
		// TODO Auto-generated method stub
		JSONObject jsonObj = null;
		String orgNo = "";
		String orgName = "";
		String teamID = "";
		String teamName = "";
		String cdate = "";
		Float accuOfArrearage = null;
		Float accuOfReceivable = null;
		Float monthOfReceivable = null;
		Float monthOfArrearage = null;
		String rateofrecovery = null;
		PowerRateRecovery prr = null;
		DecimalFormat df = new DecimalFormat("#.00");
        
		if(StringUtils.isNotBlank(content)){
		    	JSONArray jsonArr = JSON.parseArray(content);
		    	Iterator<Object> it = jsonArr.iterator();
	            while (it.hasNext()) {
	                jsonObj = (JSONObject) it.next();
	                orgNo = jsonObj.getString("orgNo");
	                orgName = jsonObj.getString("orgName");
//	                if(StringUtils.isNotBlank(orgNo) && orgNo.equals(ORG_NO)){
		                teamID = jsonObj.getString("teamID");
		                teamName = jsonObj.getString("teanName");
		                cdate = jsonObj.getString("cdate");
		                accuOfArrearage = jsonObj.getString("accuOfArrearage").equals("")?null:jsonObj.getFloat("accuOfArrearage");
		                accuOfReceivable = jsonObj.getString("accuOfReceivable").equals("")?null:jsonObj.getFloat("accuOfReceivable");
		                monthOfReceivable = jsonObj.getString("monthOfReceivable").equals("")?null:jsonObj.getFloat("monthOfReceivable");
		                monthOfArrearage = jsonObj.getString("monthOfArrearage").equals("")?null:jsonObj.getFloat("monthOfArrearage");
		                
		                prr = new PowerRateRecovery();
		                prr.setOrgNo(orgNo);
		                prr.setOrgName(orgName);
		                prr.setBzId(teamID);
		                prr.setBzName(teamName);
		                prr.setcDate(cdate);
		                prr.setArrearage(accuOfArrearage);
		                
		                
		                //累计回收率
		                Float rfrTmp = (accuOfReceivable -accuOfArrearage ) *100/accuOfReceivable ;
		                rateofrecovery = df.format(Double.parseDouble(rfrTmp.toString()));
		                prr.setRateofrecovery(Float.parseFloat(rateofrecovery));
		                prr.setAccuOfReceivable(accuOfReceivable);
		                prr.setMonthOfReceivable(monthOfReceivable);
		                prr.setMonthOfArrearage(monthOfArrearage);
		                
		                this.commonDao.save(prr);
//	                }
	            }
		 }
	}
	
	@Override
	public void saveCheckerDdata(String content) {
		// TODO Auto-generated method stub
		JSONObject jsonObj = null;
		String orgNo = "";
		String orgName = "";
		String teamID = "";
		String teamName = "";
		String cdate = "";
		String checkerName = "";
		String checkerNo = "";
		Float arrearage = null;
		Float rateofrecovery = null;
		Integer summon = null;
		Integer wos95598 = null;
		Integer wos12345 = null;
		Integer parlmExtension = null;
		Integer lowextEnsion = null;
		Integer hvUsers = null;
		Integer hvVoltageUsers = null;
		Integer industryUsers = null;
		CheckerDdata cdd = null;
		Integer userCheckers = null;
		
		DecimalFormat df = new DecimalFormat("#.00");
        
		if(StringUtils.isNotBlank(content)){
		    	JSONArray jsonArr = JSON.parseArray(content);
		    	Iterator<Object> it = jsonArr.iterator();
	            while (it.hasNext()) {
	                jsonObj = (JSONObject) it.next();
	                orgNo = jsonObj.getString("orgNo");
	                orgName = jsonObj.getString("orgName");
//	                if(StringUtils.isNotBlank(orgNo) && orgNo.equals(ORG_NO)){
		                teamID = jsonObj.getString("teamID");
		                teamName = jsonObj.getString("teanName");
		                cdate = jsonObj.getString("cdate");
		                checkerName = jsonObj.getString("checkerName");
		                checkerNo = jsonObj.getString("checkerNo");
		                arrearage = jsonObj.getFloat("arrearage");
		            	String ror = jsonObj.getString("rateOfRecovery");
		                if(StringUtils.isNotEmpty(ror)){		                
		                	if(ror.indexOf("%")!=-1){
		                		ror = ror.substring(0, ror.indexOf("%"));
		                		if(StringUtils.isNotEmpty(ror)){
		                			rateofrecovery = Float.parseFloat(ror);
		                		}
		                	}
		                }
		                
		                summon = jsonObj.getString("summon").equals("")?null:jsonObj.getInteger("summon");
		                wos95598 = jsonObj.getString("wos95598").equals("")?null:jsonObj.getInteger("wos95598");
		                wos12345 = jsonObj.getString("wos12345").equals("")?null:jsonObj.getInteger("wos12345");
		                parlmExtension = jsonObj.getString("parlmExtension").equals("")?null:jsonObj.getInteger("parlmExtension");
		                lowextEnsion = jsonObj.getString("lowextEnsion").equals("")?null:jsonObj.getInteger("lowextEnsion");
		                hvUsers = jsonObj.getString("hvUsers").equals("")?null:jsonObj.getInteger("hvUsers");
		                hvVoltageUsers = jsonObj.getString("hvVoltageUsers").equals("")?null:jsonObj.getInteger("hvVoltageUsers");
		                industryUsers = jsonObj.getString("industryUsers").equals("")?null:jsonObj.getInteger("industryUsers");
		                userCheckers = jsonObj.getString("userCheckers").equals("")?null:jsonObj.getInteger("userCheckers");
		                
		                cdd = new CheckerDdata();
		                cdd.setOrgNo(orgNo);
		                cdd.setOrgName(orgName);
		                cdd.setBzId(teamID);
		                cdd.setBzName(teamName);
		                cdd.setCreateDate(cdate);
		                cdd.setCheckerName(checkerName);   
		                cdd.setCheckerNo(checkerNo);
		                cdd.setArrearage(arrearage);
		                cdd.setRateofrecovery(rateofrecovery);
		                cdd.setSummon(summon);
		                cdd.setWos95598(wos95598);
		                cdd.setWos12345(wos12345);
		                cdd.setParlmExtension(parlmExtension);
		                cdd.setLowExtension(lowextEnsion);
		                cdd.setHvUsers(hvUsers);
		                cdd.setHvVoltageUsers(hvVoltageUsers);
		                cdd.setIndustryUsers(industryUsers);
		                cdd.setUserCheckers(userCheckers);
		                
		                this.commonDao.save(cdd);
//	                }
	             
	            }
		 }
	}
	
	@Override
	public void saveCheckerMdata(String content) {
		// TODO Auto-generated method stub
		JSONObject jsonObj = null;
		String orgNo = "";
		String orgName = "";
		String teamID = "";
		String teamName = "";
		String cmonth = "";
		String checkerName = "";
		String checkerNo = "";
 
		Integer inspections = null;
		Integer planInspections = null;
		Integer powerGuarants = null;
		Integer emergencyRepairs = null;
		String keyWork = null;
		Float settleProportion = null;
		CheckerMdata cmd = null;
		
		DecimalFormat df = new DecimalFormat("#.00");
        
		if(StringUtils.isNotBlank(content)){
		    	JSONArray jsonArr = JSON.parseArray(content);
		    	Iterator<Object> it = jsonArr.iterator();
	            while (it.hasNext()) {
	                jsonObj = (JSONObject) it.next();
	                orgNo = jsonObj.getString("orgNo");
	                orgName = jsonObj.getString("orgName");
//	                if(StringUtils.isNotBlank(orgNo) && orgNo.equals(ORG_NO)){
		                teamID = jsonObj.getString("teamID");
		                teamName = jsonObj.getString("teamName");
		                checkerName = jsonObj.getString("checkerName");
		                checkerNo = jsonObj.getString("checkerNo");
		                cmonth = jsonObj.getString("cmonth");
		                 
		                inspections = jsonObj.getString("inspections").equals("")?null:jsonObj.getInteger("inspections");
		                planInspections = jsonObj.getString("planInspections").equals("")?null:jsonObj.getInteger("planInspections");
		                powerGuarants = jsonObj.getString("powerGuarants").equals("")?null:jsonObj.getInteger("powerGuarants");
		                emergencyRepairs = jsonObj.getString("emergencyRepairs").equals("")?null:jsonObj.getInteger("emergencyRepairs");
		                keyWork = jsonObj.getString("keyWork"); 
		                settleProportion = jsonObj.getString("settleProportion").equals("")?null:jsonObj.getFloat("settleProportion"); 
		                
		                cmd = new CheckerMdata();
		                cmd.setOrgNo(orgNo);
		                cmd.setOrgName(orgName);
		                cmd.setBzId(teamID);
		                cmd.setBzName(teamName);
		                cmd.setCreateMonth(cmonth);
		                cmd.setCheckerName(checkerName);   
		                cmd.setCheckerNo(checkerNo);
		                cmd.setInspections(inspections);
		                cmd.setPlanInspections(planInspections);
		                cmd.setPowerGuarants(powerGuarants);
		                cmd.setEmergencyRepairs(emergencyRepairs);
		                cmd.setKeyWork(keyWork);
		                cmd.setSettleProportion(settleProportion);

		                this.commonDao.save(cmd);
//	                }
	             
	            }
		 }
	}

}
